﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Web;
using CommerceOne.Common;
using CommerceOne.Controllers;
using CommerceOne.Domains.Commons;

namespace CommerceOne.Widgets.Sorting
{
    public class SortingController : ControllerBase
    {
        public SortingViewModel GetViewModel(Parameters parameter)
        {
            string configPath = @"~\Configurations\ProductSorting.xml";
            configPath = this.HttpContextBase.Server.MapPath(configPath);

            var configuration = SerializeService.Instance.DeserializeObject<Configurations>(configPath);

            var viewModel = new SortingViewModel();
            if (configuration == null) throw new Exception("Config file doesnot existed. Check file: " + configPath);
            if (parameter== null) throw new Exception("Parameter cannot be null.");
            if (!parameter.ObjectId.HasValue) throw new Exception("Parameter is missing ObjectID value.");

            viewModel.SortingItems = configuration.SortingItems;

            // Get Build query item
            string queryString = string.Empty;
            string currentSelected = string.Empty;
            if (this.HttpContextBase.Request.QueryString != null)
            {
                foreach (string queryItem in
                    this.HttpContextBase.Request.QueryString.AllKeys.Where(queryItem => !queryItem.Equals(QueryStringItem.SORTING, StringComparison.OrdinalIgnoreCase)))
                {
                    if (!string.IsNullOrEmpty(queryString)) queryString += "&";
                    queryString += queryItem + "=" + this.HttpContextBase.Request.QueryString[queryItem];
                }
                currentSelected = this.HttpContextBase.Request.QueryString[QueryStringItem.SORTING];
            }

            // Make basic url
            // Example: http://www.shoes.com/category/12/1/converse
            string url = UrlBuilderService.Instance.GetUrlRoute(parameter.PageType, parameter.ObjectId.Value, 1, parameter.ObjectTitle, string.Empty);

            if (String.IsNullOrEmpty(currentSelected))
            {
                foreach (var item in viewModel.SortingItems)
                {
                    item.IsCurrent = item.IsDefault;
                    var queryStr = string.Format("{0}={1}", QueryStringItem.SORTING, item.SortingType);
                    if (!string.IsNullOrEmpty(queryString)) queryString += "&";
                    queryString += queryStr;
                    item.PostBackAddress = url + "?" + queryStr;
                    if (item.IsDefault) viewModel.CurrentSelectedType = item.SortingType;
                }
            }
            else
            {
                ProductSortType sortingType;
                var tryParsing = Enum.TryParse(currentSelected, true, out sortingType);
                if (tryParsing)
                {
                    foreach (var item in viewModel.SortingItems)
                    {
                        item.IsCurrent = (item.SortingType == sortingType);
                        string queryStr = string.Format("{0}={1}", QueryStringItem.SORTING, item.SortingType);
                        if (!string.IsNullOrEmpty(queryString)) queryString += "&";
                        queryString += queryStr;
                        item.PostBackAddress = url + "?" + queryStr;
                    }
                    viewModel.CurrentSelectedType = sortingType;
                }
                else
                {
                    foreach (var item in viewModel.SortingItems)
                    {
                        item.IsCurrent = item.IsDefault;
                        var queryStr = string.Format("{0}={1}", QueryStringItem.SORTING, item.SortingType);
                        if (!string.IsNullOrEmpty(queryString)) queryString += "&";
                        queryString += queryStr;
                        item.PostBackAddress = url + "?" + queryStr;
                        if (item.IsDefault) viewModel.CurrentSelectedType = item.SortingType;
                    }
                }
            }

            return viewModel;
        }
    }
}